home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / CEGUISDK-0.4.1-VC6-STLport.exe / {app} / Samples / common / src / CEGuiOgreBaseApplication.cpp < prev    next >
C/C++ Source or Header  |  2005-11-23  |  10KB  |  317 lines

  1. /************************************************************************
  2.     filename:   CEGuiOgreBaseApplication.cpp
  3.     created:    9/3/2004
  4.     author:     Paul D Turner
  5. *************************************************************************/
  6. /*************************************************************************
  7.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  8.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  9.  
  10.     This library is free software; you can redistribute it and/or
  11.     modify it under the terms of the GNU Lesser General Public
  12.     License as published by the Free Software Foundation; either
  13.     version 2.1 of the License, or (at your option) any later version.
  14.  
  15.     This library is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.     Lesser General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU Lesser General Public
  21.     License along with this library; if not, write to the Free Software
  22.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23. *************************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #   include "config.h"
  26. #endif
  27.  
  28. // this controls conditional compile of file for MSVC
  29. #include "CEGUIConfig.h"
  30. #ifdef CEGUI_SAMPLES_USE_OGRE
  31.  
  32. #include "CEGuiOgreBaseApplication.h"
  33. #include "CEGuiSample.h"
  34.  
  35. #include <OgreKeyEvent.h>
  36.  
  37.  
  38. CEGuiOgreBaseApplication::CEGuiOgreBaseApplication() :
  39.         d_ogreRoot(0),
  40.         d_renderer(0),
  41.         d_initialised(false),
  42.         d_frameListener(0)
  43. {
  44.     using namespace Ogre;
  45.  
  46.     d_ogreRoot = new Root();
  47.  
  48.     initialiseResources();
  49.  
  50.     if (d_ogreRoot->showConfigDialog())
  51.     {
  52.         // initialise system according to user options.
  53.         d_window = d_ogreRoot->initialise(true);
  54.  
  55.         // Create and initialise the camera
  56.         d_camera = d_ogreRoot->getSceneManager(ST_GENERIC)->createCamera("PlayerCam");
  57.         d_camera->setPosition(Vector3(0,0,500));
  58.         d_camera->lookAt(Vector3(0,0,-300));
  59.         d_camera->setNearClipDistance(5);
  60.  
  61.         // Create a viewport covering whole window
  62.         Viewport* vp = d_window->addViewport(d_camera);
  63.         vp->setBackgroundColour(ColourValue(0,0,0));
  64.  
  65.         // Update the camera aspect ratio to that of the viewport
  66.         d_camera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
  67.  
  68.         // initialise resources
  69.         ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
  70.  
  71.         // initialise GUI system
  72.         d_renderer = new CEGUI::OgreCEGUIRenderer(d_window);
  73.         new CEGUI::System(d_renderer);
  74.  
  75.         // create frame listener
  76.         d_frameListener= new CEGuiDemoFrameListener(this, d_window, d_camera);
  77.         d_ogreRoot->addFrameListener(d_frameListener);
  78.  
  79.         d_initialised = true;
  80.     }
  81.     else
  82.     {
  83.         // aborted.  Clean up and set root to 0 so when app attempts to run it knows what happened here.
  84.         delete d_ogreRoot;
  85.         d_ogreRoot = 0;
  86.     }
  87. }
  88.  
  89. CEGuiOgreBaseApplication::~CEGuiOgreBaseApplication()
  90. {
  91.     delete d_frameListener;
  92.     delete CEGUI::System::getSingletonPtr();
  93.     delete d_renderer;
  94.     delete d_ogreRoot;
  95. }
  96.  
  97. bool CEGuiOgreBaseApplication::execute(CEGuiSample* sampleApp)
  98. {
  99.     // if initialisation failed or was cancelled by user, bail out now.
  100.     if (d_ogreRoot && d_initialised)
  101.     {
  102.         // perform sample initialisation
  103.         sampleApp->initialiseSample();
  104.  
  105.         // start rendering via Ogre3D engine.
  106.         try
  107.         {
  108.             d_ogreRoot->startRendering();
  109.         }
  110.         catch(Ogre::Exception&)
  111.         {
  112.             return false;
  113.         }
  114.         catch(CEGUI::Exception&)
  115.         {
  116.             return false;
  117.         }
  118.  
  119.         return true;
  120.     }
  121.     else
  122.     {
  123.         return false;
  124.     }
  125. }
  126.  
  127. void CEGuiOgreBaseApplication::cleanup()
  128. {
  129.     // nothing to do here.
  130. }
  131.  
  132. void CEGuiOgreBaseApplication::initialiseResources(void)
  133. {
  134.     using namespace Ogre;
  135.  
  136.     // add CEGUI sample framework datafile dirs as resource locations
  137.     ResourceGroupManager::getSingleton().addResourceLocation("./", "FileSystem");
  138. #ifndef __APPLE__
  139.     ResourceGroupManager::getSingleton().addResourceLocation("../datafiles/", "FileSystem");
  140.     ResourceGroupManager::getSingleton().addResourceLocation("../datafiles/configs", "FileSystem");
  141.     ResourceGroupManager::getSingleton().addResourceLocation("../datafiles/fonts", "FileSystem");
  142.     ResourceGroupManager::getSingleton().addResourceLocation("../datafiles/imagesets", "FileSystem");
  143.     ResourceGroupManager::getSingleton().addResourceLocation("../datafiles/layouts", "FileSystem");
  144.     ResourceGroupManager::getSingleton().addResourceLocation("../datafiles/looknfeel", "FileSystem");
  145.     ResourceGroupManager::getSingleton().addResourceLocation("../datafiles/lua_scripts", "FileSystem");
  146.     ResourceGroupManager::getSingleton().addResourceLocation("../datafiles/schemes", "FileSystem");
  147. #else
  148.     // Because Ogre/Mac looks in the bundle's Resources folder by default...
  149.     ResourceGroupManager::getSingleton().addResourceLocation("datafiles/", "FileSystem");
  150.     ResourceGroupManager::getSingleton().addResourceLocation("datafiles/configs", "FileSystem");
  151.     ResourceGroupManager::getSingleton().addResourceLocation("datafiles/fonts", "FileSystem");
  152.     ResourceGroupManager::getSingleton().addResourceLocation("datafiles/imagesets", "FileSystem");
  153.     ResourceGroupManager::getSingleton().addResourceLocation("datafiles/layouts", "FileSystem");
  154.     ResourceGroupManager::getSingleton().addResourceLocation("datafiles/looknfeel", "FileSystem");
  155.     ResourceGroupManager::getSingleton().addResourceLocation("datafiles/lua_scripts", "FileSystem");
  156.     ResourceGroupManager::getSingleton().addResourceLocation("datafiles/schemes", "FileSystem");
  157. #endif
  158. }
  159.  
  160.  
  161. ////////////////////////////////////////////////////////////////////////////////
  162. /*******************************************************************************
  163.     Start of CEGuiDemoFrameListener mehods
  164. *******************************************************************************/
  165. ////////////////////////////////////////////////////////////////////////////////
  166. CEGuiDemoFrameListener::CEGuiDemoFrameListener(CEGuiBaseApplication* baseApp, Ogre::RenderWindow* window, Ogre::Camera* camera, bool useBufferedInputKeys, bool useBufferedInputMouse)
  167. {
  168.     // create and initialise events processor
  169.     d_eventProcessor = new Ogre::EventProcessor();
  170.     d_eventProcessor->initialise(window);
  171.     d_eventProcessor->addKeyListener(this);
  172.     d_eventProcessor->addMouseMotionListener(this);
  173.     d_eventProcessor->addMouseListener(this);
  174.     d_eventProcessor->startProcessingEvents();
  175.  
  176.     // store inputs we want to make use of
  177.     d_camera = camera;
  178.     d_window = window;
  179.  
  180.     // we've not quit yet.
  181.     d_quit = false;
  182.  
  183.     // setup base app ptr
  184.     d_baseApp = baseApp;
  185. }
  186.  
  187. CEGuiDemoFrameListener::~CEGuiDemoFrameListener()
  188. {
  189.     delete d_eventProcessor;
  190. }
  191.  
  192. bool CEGuiDemoFrameListener::frameStarted(const Ogre::FrameEvent& evt)
  193. {
  194.     if(d_window->isClosed() || d_quit || d_baseApp->isQuitting())
  195.     {
  196.         return false;
  197.     }
  198.     else
  199.     {
  200.         // always inject a time pulse to enable widget automation
  201.         CEGUI::System::getSingleton().injectTimePulse(static_cast<float>(evt.timeSinceLastFrame));
  202.         return true;
  203.     }
  204. }
  205.  
  206. bool CEGuiDemoFrameListener::frameEnded(const Ogre::FrameEvent& evt)
  207. {
  208.     return true;
  209. }
  210.  
  211. void CEGuiDemoFrameListener::mouseMoved(Ogre::MouseEvent *e)
  212. {
  213.     CEGUI::Renderer* rend = CEGUI::System::getSingleton().getRenderer();
  214.     CEGUI::System::getSingleton().injectMouseMove(e->getRelX() * rend->getWidth(), e->getRelY() * rend->getHeight());
  215.  
  216.     float wheel = e->getRelZ();
  217.  
  218.     if (wheel != 0)
  219.     {
  220.         CEGUI::System::getSingleton().injectMouseWheelChange(wheel * 10);
  221.     }
  222.  
  223.     e->consume();
  224. }
  225.  
  226.  
  227. void CEGuiDemoFrameListener::mouseDragged(Ogre::MouseEvent *e)
  228. {
  229.     mouseMoved(e);
  230. }
  231.  
  232.  
  233. void CEGuiDemoFrameListener::keyPressed(Ogre::KeyEvent *e)
  234. {
  235.     // give 'quitting' priority
  236.     if (e->getKey() == Ogre::KC_ESCAPE)
  237.     {
  238.         d_quit = true;
  239.         e->consume();
  240.         return;
  241.     }
  242.  
  243.     // do event injection
  244.     CEGUI::System& cegui = CEGUI::System::getSingleton();
  245.  
  246.     // key down
  247.     cegui.injectKeyDown(e->getKey());
  248.  
  249.     // now character
  250.     cegui.injectChar(e->getKeyChar());
  251.  
  252.     e->consume();
  253. }
  254.  
  255.  
  256. void CEGuiDemoFrameListener::keyReleased(Ogre::KeyEvent *e)
  257. {
  258.     CEGUI::System::getSingleton().injectKeyUp(e->getKey());
  259. }
  260.  
  261.  
  262.  
  263. void CEGuiDemoFrameListener::mousePressed(Ogre::MouseEvent *e)
  264. {
  265.     CEGUI::System::getSingleton().injectMouseButtonDown(convertOgreButtonToCegui(e->getButtonID()));
  266.     e->consume();
  267. }
  268.  
  269.  
  270. void CEGuiDemoFrameListener::mouseReleased(Ogre::MouseEvent *e)
  271. {
  272.     CEGUI::System::getSingleton().injectMouseButtonUp(convertOgreButtonToCegui(e->getButtonID()));
  273.     e->consume();
  274. }
  275.  
  276. void CEGuiDemoFrameListener::keyClicked(Ogre::KeyEvent *e)
  277. {}
  278.  
  279. void CEGuiDemoFrameListener::mouseClicked(Ogre::MouseEvent *e)
  280. {}
  281.  
  282. void CEGuiDemoFrameListener::mouseEntered(Ogre::MouseEvent *e)
  283. {}
  284.  
  285. void CEGuiDemoFrameListener::mouseExited(Ogre::MouseEvent *e)
  286. {}
  287.  
  288. CEGUI::MouseButton CEGuiDemoFrameListener::convertOgreButtonToCegui(int ogre_button_id)
  289. {
  290.     switch (ogre_button_id)
  291.     {
  292.     case Ogre::MouseEvent::BUTTON0_MASK:
  293.         return CEGUI::LeftButton;
  294.         break;
  295.  
  296.     case Ogre::MouseEvent::BUTTON1_MASK:
  297.         return CEGUI::RightButton;
  298.         break;
  299.  
  300.     case Ogre::MouseEvent::BUTTON2_MASK:
  301.         return CEGUI::MiddleButton;
  302.         break;
  303.  
  304.     case Ogre::MouseEvent::BUTTON3_MASK:
  305.         return CEGUI::X1Button;
  306.         break;
  307.  
  308.     default:
  309.         return CEGUI::LeftButton;
  310.         break;
  311.     }
  312.  
  313. }
  314.  
  315. #endif
  316.  
  317.